<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form v-show="showSearch" ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="任务名称" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入任务名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="任务类型" prop="taskType">
              <el-select v-model="queryParams.taskTypeId" placeholder="请选择任务类型" clearable @keyup.enter="handleQuery">
                <el-option v-for="item in taskOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item v-if="isVisible" label="预计开始时间" prop="expectedStartTime" label-width="100px">
              <el-date-picker
                v-model="queryParams.expectedStartTime"
                clearable
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择预计开始时间"
              />
            </el-form-item>
            <el-form-item v-if="isVisible" label="预计结束时间" prop="expectedEndTime" label-width="100px">
              <el-date-picker
                v-model="queryParams.expectedEndTime"
                clearable
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择预计结束时间"
              />
            </el-form-item>
            <el-form-item v-if="isVisible" label="实际开始时间" prop="actualStartTime" label-width="100px">
              <el-date-picker
                v-model="queryParams.actualStartTime"
                clearable
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择实际开始时间"
              />
            </el-form-item>
            <el-form-item v-if="isVisible" label="实际结束时间" prop="actualEndTime" label-width="100px">
              <el-date-picker v-model="queryParams.actualEndTime" clearable type="date" value-format="YYYY-MM-DD" placeholder="请选择实际结束时间" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>
    <el-card shadow="hover">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <right-toolbar v-model:showSearch="showSearch" @query-table="handleQuery"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="taskList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column align="center" type="index" label="序号" width="60"></el-table-column>
        <el-table-column align="center" prop="taskName" label="任务名称"></el-table-column>
        <el-table-column align="center" prop="taskTypeName" label="任务类型"></el-table-column>
        <el-table-column align="center" prop="userType" label="用户类型">
          <template #default="scope">
            {{ user_type.find((item) => item.value === scope.row.userType)?.label }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="handlerUserName" label="任务负责人"></el-table-column>
        <el-table-column align="center" prop="taskStatus" label="任务状态">
          <template #default="scope">
            {{ task_status.find((item) => item.value === scope.row.taskStatus)?.label }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="expectedStartTime" label="预计开始时间"></el-table-column>
        <el-table-column align="center" prop="expectedEndTime" label="预计结束时间"></el-table-column>
        <el-table-column align="center" prop="actualStartTime" label="实际开始时间"></el-table-column>
        <el-table-column align="center" prop="actualEndTime" label="实际结束时间"></el-table-column>
        <el-table-column align="center" label="流程状态" min-width="70">
          <template #default="scope">
            <dict-tag :options="wf_business_status" :value="scope.row.processStatus"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="createTime" label="创建时间" width="160"></el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <template #default="scope">
            <el-button type="primary" size="small" icon="Edit" @click="handleOpen(scope.row)">办理</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        :total="total"
        @pagination="handleQuery"
      />
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { getPageByTaskWait } from '@/api/workflow/task';
import { TaskQuery, TaskVO } from '@/api/workflow/task/types';
import workflowCommon from '@/api/workflow/workflowCommon';
import { RouterJumpVo } from '@/api/workflow/workflowCommon/types';
import { listTaskType } from '@/api/task/taskType';
import PersonSelect from '@/views/target/person/PersonSelect.vue';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { wf_business_status, user_type, task_status } = toRefs<any>(proxy?.useDict('wf_business_status', 'user_type', 'task_status'));
//提交组件
const queryFormRef = ref<ElFormInstance>();

const isVisible = ref(true);
const taskOptions = ref([]);
// 遮罩层
const loading = ref(true);
// 选中数组
const ids = ref<Array<any>>([]);
// 非单个禁用
const single = ref(true);
// 非多个禁用
const multiple = ref(true);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
// 模型定义表格数据
const taskList = ref([]);
// 查询参数
const queryParams = ref<TaskQuery>({
  pageNum: 1,
  pageSize: 10,
  name: undefined,
  processDefinitionName: undefined,
  processDefinitionKey: undefined,
  expectedStartTime: undefined,
  expectedEndTime: undefined
});

/** 查询任务列表 */
const getTaskTypeList = async () => {
  loading.value = true;
  const res = await listTaskType();
  taskOptions.value = res.rows;
  loading.value = false;
};
onMounted(() => {
  getWaitingList();
  getTaskTypeList();
});
/** 搜索按钮操作 */
const handleQuery = () => {
  getWaitingList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  queryParams.value.pageNum = 1;
  queryParams.value.pageSize = 10;
  handleQuery();
};
// 多选框选中数据
const handleSelectionChange = (selection: any) => {
  ids.value = selection.map((item: any) => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
};
//分页
const getWaitingList = () => {
  loading.value = true;
  getPageByTaskWait(queryParams.value).then((resp) => {
    taskList.value = resp.rows;
    total.value = resp.total;
    loading.value = false;
  });
};
//办理
const handleOpen = async (row: TaskVO) => {
  const routerJumpVo = reactive<RouterJumpVo>({
    businessKey: row.businessKey,
    processInstanceId: row.processInstanceId,
    type: 'approval'
  });
  proxy.$router.push({
    // 这个路径不知道怎么去跳转的
    path: `/workflow/task/index`,
    query: {
      id: routerJumpVo.businessKey,
      // type: routerJumpVo.type,
      taskId: routerJumpVo.taskId,
      processInstanceId: routerJumpVo.processInstanceId,
      type: 'approval'
    }
  });
};
</script>
